package com.dehui.traders.ui.stockdetail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import com.dehui.traders.BR;
import com.dehui.traders.R;
import com.dehui.traders.bean.PredictBean;
import com.dehui.traders.config.Config;
import com.dehui.traders.ui.stockdetail.adapter.FinancialLiRunAdapter;
import com.dehui.traders.ui.stockdetail.adapter.FinancialZhibiaoAdapter;
import com.dehui.traders.ui.stockdetail.adapter.FinanialFuzaiAdapter;
import com.dehui.traders.ui.stockdetail.adapter.StockCaptialAdapter;
import com.dehui.traders.bean.Announcement;
import com.dehui.traders.bean.F10Bean;
import com.dehui.traders.bean.FinancialBean;
import com.dehui.traders.bean.FinancialData;
import com.dehui.traders.bean.NewBean;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.bean.ShareHolderBean;
import com.dehui.traders.ui.stockdetail.adapter.StocksDetailSyAdapter;
import com.dehui.traders.ui.stockdetail.bean.LabelTabel;
import com.dehui.traders.ui.stockdetail.bean.StockCapital;
import com.dehui.traders.databinding.ActivityStocksDetailBinding;
import com.dehui.traders.kchart.DataParse;
import com.dehui.traders.kchart.KLineBean;
import com.dehui.traders.kchart.MinutesBean;
import com.dehui.traders.ui.web.WebDetailActivity;
import com.dehui.traders.ui.dialog.PanKInfoView;
import com.dehui.traders.ui.dialog.SelectKPopView;
import com.dehui.traders.ui.dialog.ZhoukPopView;
import com.dehui.traders.ui.stockdetail.chart.FenKDetailFragment;
import com.dehui.traders.ui.stockdetail.chart.FenShiDetailFragment;
import com.dehui.traders.ui.stockdetail.chart.KDetailFragment;
import com.dehui.traders.ui.stockdetail.chart.ZhouKDetailFragment;
import com.dehui.traders.utils.DateUtil;
import com.dehui.traders.utils.SimulatedDataManager;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import com.dehui.traders.widgets.DetailTableView;
import com.dehui.traders.widgets.MyHScrollView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;
import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.2.
 */

public class StocksDetailActivity extends BaseActivity<ActivityStocksDetailBinding, StocksDetailViewModel> {

    public static final String IntentKey_LeftName = "IntentKey_LeftName";
    public static final String IntentKey_StocksName = "IntentKey_StocksName";
    public static final String IntentKey_StockCode = "IntentKey_StockCode";
    public static final String IntentKey_ClosePrice = "IntentKey_ClosePrice";
    public static final String IntentKey_StockMarket = "IntentKey_StockMarket";
    public static final String IntentKey_Type = "IntentKey_Type";

    private static final int FenShi = 0;
    private static final int SixFen = 1;
    private static final int RiK = 2;
    private static final int ZhouK = 3;
    private int currentPos = FenShi;
    private int mType = 0;

    private String mStockName;
    public HorizontalScrollView mTouchView;
    private List<NewBean> newsList = new ArrayList<>();
    private List<Announcement> announcementList = new ArrayList<>();
    private CommonAdapter<NewBean> mNewAdapter;
    private CommonAdapter<Announcement> mAnnouncementAdapter;
    private F10Bean mF10Bean;
    private ShareHolderBean mShareHolderBean;
    private FinancialBean mFinancialBean;
    private StocksDetailViewModel model;
    private String mStockCode;
    private String mLeftName;
    private RealtimeQuote mRealTimeQuote;
    private String mClosePrice;
    private String mStockMarket;
    private TextView lastClickButton;
    private boolean isHaveFiveView;
    private Fragment oldFragment;
    private FenShiDetailFragment mFenShiDetailFragment;
    private KDetailFragment mKDetailFragment;
    private FenKDetailFragment mFenKDetailFragemnt;
    private ZhouKDetailFragment mZhouKDetailFragment;
    private SuspendView suspendView;
    private ScrollView svDetails;
    private DetailTableView tabView;

    /**
     *
     * @param context
     * @param title
     * @param code
     * @param closePrice
     * @param market
     */
    public static void start(Context context, String leftName, String title, String code, String closePrice, String market){
        Intent intent = new Intent(context,StocksDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(StocksDetailActivity.IntentKey_LeftName,leftName);
        bundle.putString(StocksDetailActivity.IntentKey_StocksName,title);
        bundle.putString(StocksDetailActivity.IntentKey_StockCode,code);
        bundle.putString(StocksDetailActivity.IntentKey_ClosePrice,closePrice);
        bundle.putString(StocksDetailActivity.IntentKey_StockMarket,market);
        intent.putExtra("bundle",bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     *
     * @param activity
     * @param leftName
     * @param title
     * @param code
     * @param closePrice
     * @param market
     */
    public static void startType(Activity activity,String leftName,String title,String code,String closePrice,String market,int type){
        Intent intent = new Intent(activity,StocksDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(StocksDetailActivity.IntentKey_LeftName,leftName);
        bundle.putString(StocksDetailActivity.IntentKey_StocksName,title);
        bundle.putString(StocksDetailActivity.IntentKey_StockCode,code);
        bundle.putString(StocksDetailActivity.IntentKey_ClosePrice,closePrice);
        bundle.putString(StocksDetailActivity.IntentKey_StockMarket,market);
        bundle.putInt(StocksDetailActivity.IntentKey_Type,type);
        intent.putExtra("bundle",bundle);
        activity.startActivity(intent);
    }


    @Override
    public int initContentView() {
        return R.layout.activity_stocks_detail;
    }

    @Override
    public void initParam() {
        Bundle bundle = getIntent().getBundleExtra("bundle");
        mStockName = bundle.getString(IntentKey_StocksName);
        mStockCode = bundle.getString(IntentKey_StockCode);
        mLeftName = bundle.getString(IntentKey_LeftName);
        mClosePrice = bundle.getString(IntentKey_ClosePrice);
        mStockMarket = bundle.getString(IntentKey_StockMarket);
        mType = bundle.getInt(IntentKey_Type);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            landscapeChange();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            portrait();
        }
    }

    /**
     * 竖屏状态
     */
    private void portrait() {
        binding.includeTop.llDetailTop.setVisibility(View.VISIBLE);
        binding.includeTab.llTab.setVisibility(View.VISIBLE);
        if (mType == 0){
            svDetails.setVisibility(View.VISIBLE);
            binding.flContainer.setVisibility(View.VISIBLE);
        }else{
            binding.llIndex.setVisibility(View.VISIBLE);
        }
        binding.toolbar.setVisibility(View.VISIBLE);
        binding.includeChart.ivChartAmplification.setVisibility(View.VISIBLE);
        binding.includeChart.ivChartClose.setVisibility(View.GONE);
        if (isHaveFiveView){
            if (mType == 0)
                binding.includeChart.llWudang.setVisibility(View.VISIBLE);
        }
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) binding.includeChart.llDeatailChart.getLayoutParams();
        lp.height = Utils.dp2px(this,260);
        binding.includeChart.llDeatailChart.setLayoutParams(lp);
    }

    /**
     * 横屏状态
     */
    private void landscapeChange() {
        binding.includeTop.llDetailTop.setVisibility(View.GONE);
        svDetails.setVisibility(View.GONE);
        binding.includeTab.llTab.setVisibility(View.GONE);
        binding.flContainer.setVisibility(View.GONE);
        binding.toolbar.setVisibility(View.GONE);
        binding.includeChart.ivChartAmplification.setVisibility(View.GONE);
        binding.includeChart.ivChartClose.setVisibility(View.VISIBLE);
        if(currentPos == FenShi){
            isHaveFiveView = true;
            binding.includeChart.llWudang.setVisibility(View.GONE);
        }
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) binding.includeChart.llDeatailChart.getLayoutParams();
        lp.height = Utils.getScreenHeight(this) - Utils.getStatusHeight(this);
        binding.includeChart.llDeatailChart.setLayoutParams(lp);
        binding.llIndex.setVisibility(View.GONE);

    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StocksDetailViewModel initViewModel() {
        model = new StocksDetailViewModel(this, this,mStockName,mStockCode,mStockMarket,mType);
        return model;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
        initUI();
        initDetail();
        viewModel.getData();
        switchFenShi();
    }



    public void showKView(int index, DataParse mData) {
        KLineBean kItem = mData.getKItem(index);
        binding.includeTab.llKInfo.setVisibility(View.VISIBLE);
        binding.includeTab.tvKTime.setText(kItem.date);
        binding.includeTab.tvKOpen.setText(Utils.doubleToString(kItem.open));
        binding.includeTab.tvKHeigh.setText(Utils.doubleToString(kItem.high));
        binding.includeTab.tvKLow.setText(Utils.doubleToString(kItem.low));
        binding.includeTab.tvKClose.setText(Utils.doubleToString(kItem.close));

    }

    public void showMinuteView(int xIndex, DataParse mData) {
        MinutesBean minuterItem = mData.getMinuterItem(xIndex);
        binding.includeTab.llChartInfo.setVisibility(View.VISIBLE);
        binding.includeTab.tvMinuteTime.setText(minuterItem.time);
        binding.includeTab.tvMinutePrice.setText(Utils.doubleToString(minuterItem.cjprice));
        binding.includeTab.tvMinuteJunjia.setText(Utils.doubleToString(minuterItem.avprice));
        binding.includeTab.tvMinuteChengjiaoshou.setText(Utils.doubleToStringZ(minuterItem.cjnum / 100));
        float num = minuterItem.cjprice - Float.valueOf(mClosePrice);
        binding.includeTab.tvMinuteZhangfu.setText(Utils.doubleToString(num / Float.valueOf(mClosePrice)) + "%");
        binding.includeTop.tvTop1.setText(Utils.doubleToString(minuterItem.cjprice));
        binding.includeTop.tvTop2.setText(Utils.doubleToString(minuterItem.cjprice - Float.valueOf(mClosePrice)));
        binding.includeTop.tvTop3.setText(Utils.doubleToString(num / Float.valueOf(mClosePrice)) + "%");
    }

    /**
     * 头部数据
     * @param
     */
    public void initTopData(RealtimeQuote reealTimeQuote) {
        if (reealTimeQuote != null)
            this.mRealTimeQuote = reealTimeQuote;
        int red = getResources().getColor(R.color.red);
        int green = getResources().getColor(R.color.green);
        binding.includeTop.tvTop1.setText(Utils.doubleToString(mRealTimeQuote.price));
        binding.includeTop.tvTop2.setText(Utils.doubleToString(mRealTimeQuote.price - mRealTimeQuote.closePrice));
        float s = (mRealTimeQuote.price - mRealTimeQuote.closePrice) * 100 / (float) mRealTimeQuote.closePrice;
        binding.includeTop.tvTop3.setText(Utils.doubleToString(s) + "%");
        binding.includeTop.tvTop4.setText(Utils.doubleToString(mRealTimeQuote.openPrice));
        binding.includeTop.tvTop5.setText(Utils.doubleToString(mRealTimeQuote.highPrice));
        binding.includeTop.tvTop6.setText(Utils.doubleToString(mRealTimeQuote.closePrice));
        binding.includeTop.tvTop7.setText(Utils.doubleToString(mRealTimeQuote.lowPrice));
        Double val = Double.valueOf(mRealTimeQuote.volume);
        binding.includeTop.tvTop8.setText(Utils.doubleToString(val / 1000000) + "万");
        if (mRealTimeQuote.price - mRealTimeQuote.openPrice > 0){
            binding.includeTop.tvTop1.setTextColor(red);
            binding.includeTop.tvTop2.setTextColor(red);
            binding.includeTop.tvTop3.setTextColor(red);
            binding.includeTop.tvTop4.setTextColor(red);
            binding.includeTop.tvTop5.setTextColor(red);
            binding.includeTop.tvTop7.setTextColor(red);
        }else if (mRealTimeQuote.price - mRealTimeQuote.openPrice < 0){
            binding.includeTop.tvTop1.setTextColor(green);
            binding.includeTop.tvTop2.setTextColor(green);
            binding.includeTop.tvTop3.setTextColor(green);
            binding.includeTop.tvTop4.setTextColor(green);
            binding.includeTop.tvTop5.setTextColor(green);
            binding.includeTop.tvTop7.setTextColor(green);
        }else {
            binding.includeTop.tvTop1.setTextColor(Color.WHITE);
            binding.includeTop.tvTop2.setTextColor(Color.WHITE);
            binding.includeTop.tvTop3.setTextColor(Color.WHITE);
            binding.includeTop.tvTop4.setTextColor(Color.WHITE);
            binding.includeTop.tvTop5.setTextColor(Color.WHITE);
            binding.includeTop.tvTop6.setTextColor(Color.WHITE);
            binding.includeTop.tvTop7.setTextColor(Color.WHITE);
        }
    }


    public String getmLeftName() {
        return mLeftName;
    }

    private void initDetail() {
        initShareHolderDetail();
    }

    private void initTop() {
        binding.includeTop.llDetailTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PanKInfoView infoView = new PanKInfoView(StocksDetailActivity.this,mRealTimeQuote);
                infoView.show();
            }
        });
    }

    public float getClosePrice(){
        return Float.valueOf(mClosePrice);
    }

    private void initShareHolderDetail() {

    }


    private void initUI() {
        svDetails = findViewById(R.id.sv_details);
        tabView = findViewById(R.id.tab_view);
        initTabLayout();
        initListView();

        binding.includeFinancial.lvZhibiao.setFocusable(false);
        binding.includeFinancial.lvFuzai.setFocusable(false);
        binding.includeFinancial.lvLirun.setFocusable(false);
        binding.lvIndex.setFocusable(false);
        svDetails.setFocusable(false);

        initTop();
        initTab();

        binding.includeChart.ivChartAmplification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        });
        binding.includeChart.ivChartClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        });

        if (mType == 1){
            binding.tablayoutBottomStocksDetail.setVisibility(View.GONE);
            binding.flContainer.setVisibility(View.GONE);
            binding.llIndex.setVisibility(View.VISIBLE);
            binding.includeChart.llWudang.setVisibility(View.GONE);
        }else{
            binding.tablayoutBottomStocksDetail.setVisibility(View.GONE);
            binding.flContainer.setVisibility(View.GONE);
            binding.llIndex.setVisibility(View.GONE);
            svDetails.setVisibility(View.VISIBLE);
        }

    }

    private void initTab() {
        lastClickButton = binding.includeTab.tvTabFenshi;
        binding.includeTab.tvTabFenshi.setSelected(true);

        binding.includeTab.tvTabFenshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastClickButton != null)
                    lastClickButton.setSelected(false);
                binding.includeTab.tvTabFenshi.setSelected(true);
                lastClickButton = binding.includeTab.tvTabFenshi;
                currentPos = FenShi;
                if (mType == 1)
                    binding.includeChart.llWudang.setVisibility(View.GONE);
                else
                    binding.includeChart.llWudang.setVisibility(View.VISIBLE);
                switchFenShi();
            }
        });
        binding.includeTab.tvTab60fen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastClickButton != null)
                    lastClickButton.setSelected(false);
                binding.includeTab.tvTab60fen.setSelected(true);
                lastClickButton = binding.includeTab.tvTab60fen;
                currentPos = SixFen;
                show60FenView();
            }
        });
        binding.includeTab.tvTabRik.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastClickButton != null)
                    lastClickButton.setSelected(false);
                binding.includeTab.tvTabRik.setSelected(true);
                lastClickButton = binding.includeTab.tvTabRik;
                currentPos = RiK;
                binding.includeChart.llWudang.setVisibility(View.GONE);
                switchRik();
            }
        });
        binding.includeTab.tvTabZhouk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lastClickButton != null)
                    lastClickButton.setSelected(false);
                binding.includeTab.tvTabZhouk.setSelected(true);
                lastClickButton = binding.includeTab.tvTabZhouk;
                currentPos = ZhouK;
                showZhouKView();
            }
        });
        binding.includeTab.tvTabBs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.includeTab.tvTabBs.setSelected(!binding.includeTab.tvTabBs.isSelected());
                if (mKDetailFragment != null)
                    mKDetailFragment.setBs(binding.includeTab.tvTabBs.isSelected());
                if (mZhouKDetailFragment != null)
                    mZhouKDetailFragment.setBs(binding.includeTab.tvTabBs.isSelected());
            }
        });
        binding.includeTab.tvTabBs.setSelected(true);
    }

    private void showZhouKView() {
        ZhoukPopView zPopView = new ZhoukPopView(this);
        zPopView.setOnKZhouClick(new ZhoukPopView.OnKZhouClick() {
            @Override
            public void onzhouClick(int i) {
                binding.includeChart.llWudang.setVisibility(View.GONE);
                if (i == 0){
                    binding.includeTab.tvTabZhouk.setText("周K");
                    switchZhouK(0);
                }else if (i == 1){
                    binding.includeTab.tvTabZhouk.setText("月K");
                }
            }
        });
        zPopView.show(binding.includeTab.tvTabZhouk);
    }


    private void show60FenView() {
        SelectKPopView kPopView = new SelectKPopView(this);
        kPopView.setOnKFenClick(new SelectKPopView.OnKFenClick() {
            @Override
            public void onFenClick(int i) {
                binding.includeChart.llWudang.setVisibility(View.GONE);
                if (i == 5){
                    binding.includeTab.tvTab60fen.setText("5分");
                    switchFenK(1);
                }else if (i == 15){
                    binding.includeTab.tvTab60fen.setText("15分");
                    switchFenK(2);
                }else if (i == 30){
                    binding.includeTab.tvTab60fen.setText("30分");
                    switchFenK(3);
                }else if (i == 60){
                    binding.includeTab.tvTab60fen.setText("60分");
                    switchFenK(4);
                }

            }
        });
        kPopView.show(binding.includeTab.tvTab60fen);
    }

    private void switchFenK(int i) {
        if (mFenKDetailFragemnt == null){
            mFenKDetailFragemnt = new FenKDetailFragment();
            Bundle bunld = new Bundle();
            bunld.putString("code",mStockCode);
            bunld.putString("market",mStockMarket);
            bunld.putString("close",mClosePrice);
            bunld.putInt("fen",i);
            mFenKDetailFragemnt.setArguments(bunld);
        }else{
            mFenKDetailFragemnt.setChangeFen(i);
        }
        switchFragment(mFenKDetailFragemnt);
    }

    private void switchZhouK(int i) {
        if (mZhouKDetailFragment == null){
            mZhouKDetailFragment = new ZhouKDetailFragment();
            Bundle bunld = new Bundle();
            bunld.putString("code",mStockCode);
            bunld.putString("market",mStockMarket);
            bunld.putString("close",mClosePrice);
            bunld.putInt("zhou",i);
            mZhouKDetailFragment.setArguments(bunld);
        }
        switchFragment(mZhouKDetailFragment);
    }

    /**
     * 切换到分时
     */
    private void switchFenShi() {
        if (mFenShiDetailFragment == null){
            mFenShiDetailFragment = new FenShiDetailFragment();
            Bundle bunld = new Bundle();
            bunld.putString("code",mStockCode);
            bunld.putString("market",mStockMarket);
            bunld.putString("close",mClosePrice);
            mFenShiDetailFragment.setArguments(bunld);
        }

        switchFragment(mFenShiDetailFragment);
    }

    private void switchFragment(Fragment fragment) {
        if (oldFragment != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null)
                transaction.hide(oldFragment);
            oldFragment = fragment;
            if (!fragment.isAdded()) {
                transaction.add(R.id.fl_container, fragment).show(fragment).commitAllowingStateLoss();
            } else {
                transaction.show(fragment).commitAllowingStateLoss();
            }
            oldFragment = fragment;
        }
    }

        /**
         * 切换到日k
         */
    private void switchRik() {
        if (mKDetailFragment == null){
            mKDetailFragment = new KDetailFragment();
            Bundle bunld = new Bundle();
            bunld.putString("code",mStockCode);
            bunld.putString("market",mStockMarket);
            bunld.putString("close",mClosePrice);
            mKDetailFragment.setArguments(bunld);
        }
        switchFragment(mKDetailFragment);
    }

    @Override
    public void initData() {
        mF10Bean = SimulatedDataManager.getF10Bean();
        mShareHolderBean = SimulatedDataManager.getShareHolderBean();
        mFinancialBean = SimulatedDataManager.getFinancialBean();
    }


    private void initListView() {
        mNewAdapter = new CommonAdapter<NewBean>(this, newsList, R.layout.list_item_stocks_detail_news) {
            @Override
            public void convert(BaseViewHolder helper, NewBean item, int position) {
                TextView tvTitle = helper.getView(R.id.tv_news_title);
                TextView tvTime = helper.getView(R.id.tv_new_time);
                tvTitle.setText(item.getTitle());
                tvTime.setText(item.getTime());
            }
        };

        binding.lvStocksDetail.setAdapter(mNewAdapter);
        binding.lvStocksDetail.setFocusable(false);
    }

    private void initTabLayout() {


        List<String> list_bottom_title = Arrays.asList(getResources().getStringArray(R.array.stock_bottom_detail_tab));
        binding.tablayoutBottomStocksDetail.setTabMode(TabLayout.MODE_FIXED);
        for (String title: list_bottom_title){
            binding.tablayoutBottomStocksDetail.addTab(binding.tablayoutBottomStocksDetail.newTab().setText(title));
        }


        binding.tablayoutBottomStocksDetail.addOnTabSelectedListener(bottomTabSelectLister);

    }

    private TabLayout.OnTabSelectedListener bottomTabSelectLister = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()){
                case 0:
                    binding.lvStocksDetail.setVisibility(View.VISIBLE);
                    binding.lvStocksDetail.setAdapter(mNewAdapter);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
                case 1:
                    binding.lvStocksDetail.setVisibility(View.VISIBLE);
                    binding.lvStocksDetail.setAdapter(mAnnouncementAdapter);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
                case 2:
                    binding.lvStocksDetail.setVisibility(View.GONE);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.GONE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    binding.lvStocksDetail.setVisibility(View.GONE);
                    binding.includeF10.rlStockDetailF10.setVisibility(View.GONE);
                    binding.includeShareholders.llStockDetailShareholders.setVisibility(View.VISIBLE);
                    binding.includeFinancial.llStockDetailFinancial.setVisibility(View.GONE);
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };

    public void initFiveView() {
        binding.includeChart.tvMai5.setText(Utils.StringToString(mRealTimeQuote.sell_5));
        binding.includeChart.tvMai4.setText(Utils.StringToString(mRealTimeQuote.sell_4));
        binding.includeChart.tvMai3.setText(Utils.StringToString(mRealTimeQuote.sell_3));
        binding.includeChart.tvMai2.setText(Utils.StringToString(mRealTimeQuote.sell_2));
        binding.includeChart.tvMai1.setText(Utils.StringToString(mRealTimeQuote.sell_1));
        binding.includeChart.tvMai5S.setText(mRealTimeQuote.sell_5_s);
        binding.includeChart.tvMai4S.setText(mRealTimeQuote.sell_4_s);
        binding.includeChart.tvMai3S.setText(mRealTimeQuote.sell_3_s);
        binding.includeChart.tvMai2S.setText(mRealTimeQuote.sell_2_s);
        binding.includeChart.tvMai1S.setText(mRealTimeQuote.sell_1_s);
        binding.includeChart.tvBuy1.setText(Utils.StringToString(mRealTimeQuote.buy_1));
        binding.includeChart.tvBuy2.setText(Utils.StringToString(mRealTimeQuote.buy_2));
        binding.includeChart.tvBuy3.setText(Utils.StringToString(mRealTimeQuote.buy_3));
        binding.includeChart.tvBuy4.setText(Utils.StringToString(mRealTimeQuote.buy_4));
        binding.includeChart.tvBuy5.setText(Utils.StringToString(mRealTimeQuote.buy_5));
        binding.includeChart.tvBuy1S.setText(mRealTimeQuote.buy_1_s);
        binding.includeChart.tvBuy2S.setText(mRealTimeQuote.buy_2_s);
        binding.includeChart.tvBuy3S.setText(mRealTimeQuote.buy_3_s);
        binding.includeChart.tvBuy4S.setText(mRealTimeQuote.buy_4_s);
        binding.includeChart.tvBuy5S.setText(mRealTimeQuote.buy_5_s);
        int green = getResources().getColor(R.color.green);
        binding.includeChart.tvMai5.setTextColor(green);
        binding.includeChart.tvMai4.setTextColor(green);
        binding.includeChart.tvMai3.setTextColor(green);
        binding.includeChart.tvMai2.setTextColor(green);
        binding.includeChart.tvMai1.setTextColor(green);
        binding.includeChart.tvBuy1.setTextColor(green);
        binding.includeChart.tvBuy2.setTextColor(green);
        binding.includeChart.tvBuy3.setTextColor(green);
        binding.includeChart.tvBuy4.setTextColor(green);
        binding.includeChart.tvBuy5.setTextColor(green);
    }

    public void dimissMinuteView() {
        binding.includeTab.llChartInfo.setVisibility(View.GONE);
        initTopData(null);
    }

    public void dimissKView() {
        binding.includeTab.llKInfo.setVisibility(View.GONE);
        initTopData(null);
    }

    public void updateAnnouncementList(List<Announcement> announcementList) {
        this.announcementList = announcementList;
        mAnnouncementAdapter = new CommonAdapter<Announcement>(this, announcementList, R.layout.list_item_stocks_detail_news) {

            @Override
            public void convert(BaseViewHolder helper, Announcement item, int position) {
                TextView tvTitle = helper.getView(R.id.tv_news_title);
                TextView tvTime = helper.getView(R.id.tv_new_time);
                LinearLayout llContainer = helper.getView(R.id.ll_container);
                tvTitle.setText(item.getF002v());
                tvTime.setText(DateUtil.getStringByFormat(Long.valueOf(item.getCreateDate()), DateUtil.dateFormatYMD));

                llContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        WebDetailActivity.start(StocksDetailActivity.this,item.getF002v(),item.getF003v());
                    }
                });
            }
        };
    }

    public void initFinancial1(String[] f1Titles, FinancialData f1, String[] f2Titles, FinancialData f2, String[] f3Titles, FinancialData f3) {
        List<String> title1s = Arrays.asList(f1Titles);
        List<String> title2s = Arrays.asList(f2Titles);
        List<String> title3s = Arrays.asList(f3Titles);
        FinancialLiRunAdapter adapter1 = new FinancialLiRunAdapter(this, title1s, R.layout.list_item_financial,f1);
        binding.includeFinancial.lvLirun.setAdapter(adapter1);

        FinanialFuzaiAdapter adapter2 = new FinanialFuzaiAdapter(this, title2s, R.layout.list_item_financial, f2);
        binding.includeFinancial.lvFuzai.setAdapter(adapter2);

        FinancialZhibiaoAdapter adapter3 = new FinancialZhibiaoAdapter(this, title3s, R.layout.list_item_financial, f3);
        binding.includeFinancial.lvZhibiao.setAdapter(adapter3);

    }

    public void initStockCaptial(List<StockCapital> result) {
        StockCaptialAdapter adapter = new StockCaptialAdapter(this, result, R.layout.list_item_stock_captial);
        binding.lvIndex.setAdapter(adapter);
    }

    public void initLabelTabel(List<LabelTabel> result) {
        String[] totals = initTotals(result);
        StocksDetailSyAdapter adapter = new StocksDetailSyAdapter(this, result, this);
        tabView.setTotals(totals)
                .setOthers(Config.detailOther)
                .setAdapter(adapter)
                .setActivity(this).build();

        suspendView = new SuspendView(this, binding.rlContent,mStockMarket + mStockCode,mStockName);
        suspendView.show();
    }

    private String[] initTotals(List<LabelTabel> list) {
        String[] totals = new String[7];
        double total0 = 0;
        double total1 = 0;
        double total2 = 0;
        double total3 = 0;
        double total4 = 0;
        for (LabelTabel item : list){
            String dp0 = item.getDifferenceAveragePrice();
            String dp1 = item.getDifferenceOpenPrice();
            String dp2 = item.getDifferenceHighPrice();
            String dp3 = item.getDifferenceLowPrice();
            String dp4 = item.getDifferencePrice();

            if (!TextUtils.isEmpty(dp0)){
                try {
                    String ss = dp0.replace("%", "");
                    double doub = Double.valueOf(ss);
                    total0 += doub;
                }catch (Exception e){
                }
            }

            if (!TextUtils.isEmpty(dp1)){
                try {
                    String ss = dp1.replace("%", "");
                    double doub = Double.valueOf(ss);
                    total1 += doub;
                }catch (Exception e){
                }
            }

            if (!TextUtils.isEmpty(dp2)){
                try {
                    String ss = dp2.replace("%", "");
                    double doub = Double.valueOf(ss);
                    total2 += doub;
                }catch (Exception e){
                }
            }

            if (!TextUtils.isEmpty(dp3)){
                try {
                    String ss = dp3.replace("%", "");
                    double doub = Double.valueOf(ss);
                    total3 += doub;
                }catch (Exception e){
                }
            }

            if (!TextUtils.isEmpty(dp4)){
                try {
                    String ss = dp4.replace("%", "");
                    double doub = Double.valueOf(ss);
                    total4 += doub;
                }catch (Exception e){
                }
            }

        }

        String average = list.get(list.size() - 1).getAverage();

        if (mRealTimeQuote != null){
            String price = Utils.doubleToString(mRealTimeQuote.price);
            totals[0] = average + ","+ price + ",";
            double s = (mRealTimeQuote.price - Float.valueOf(average)) * 100 / Float.valueOf(average);
            totals[1] = Utils.doubleToString(s) + "%";
        }
        totals[2] = Utils.doubleToString(total0) + "%";
        totals[3] = Utils.doubleToString(total1) + "%";
        totals[4] = Utils.doubleToString(total2) + "%";
        totals[5] = Utils.doubleToString(total3) + "%";
        totals[6] = Utils.doubleToString(total4) + "%";

        return totals;
    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        List<MyHScrollView> mHScrollViews = tabView.getScrollViews();
        for(MyHScrollView scrollView : mHScrollViews) {
            if(mTouchView != scrollView)
                scrollView.smoothScrollTo(l, t);
        }
    }
}
